<script>
	let wxjssdk = require('@/api/WxJSSDK')
	export default {
		onLaunch: function(options) {
			// uni.setStorageSync('token', '5213fce034c877fb1af81d3086d25bc2')
			if (options.query.code) {
				this.getToken(options.query.code)
			} else {
				if (!uni.getStorageSync('token')) {
					let url = location.href.split('#')[0]
					location.href =
						`https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf8ca04889fd97fbc&redirect_uri=${url}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect`
				} else { 
					this.getUserInfo()
				}
			}

		},
		onShow: function() {
			console.log('App Show')
			this.getWxConfig()
		},
		methods: {
			getWxConfig() {
				this.$https.post('wechat/jssdkConfig').then(res => {
					wxjssdk.config({
						debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
						appId: res.appId, // 必填，公众号的唯一标识
						timestamp: res.timestamp, // 必填，生成签名的时间戳
						nonceStr: res.nonceStr, // 必填，生成签名的随机串
						signature: res.signature, // 必填，签名，见附录1
						jsApiList: ['chooseImage','uploadImage','chooseWXPay']
					})
				})
			},
			getToken(code) {
				this.$https.post('wechat/officialAccountLogin', {
					code: code
				}).then(res => {
					uni.setStorageSync('token', res.token);
					this.$emit('getRoute')
					this.getUserInfo()
				})
			},
			getUserInfo() {
				this.$https.get('user').then(res => {
					uni.setStorageSync('auth_status', res.auth ? res.auth.status : -1); //实名认证
					uni.setStorageSync('phone',res.phone)
					
					uni.setStorageSync('car_auth_status', res.car_auth ? res.car_auth.status : -1); //车主认证
				}).catch(err => {
					if (err == -1) {
						location.href = this.$imgurl + "/wechat/auth?callback_url=" + location.href.split('?')[0]
					}
				})
			}
		},
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>

<style>
	/*每个页面公共css */
	.flex {
		display: flex;
		align-items: center;
	}

	.flex-between {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.flex-around {
		display: flex;
		justify-content: space-around;
		align-items: center;
	}

	.flex-center {
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.flex-center-column {
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
	}
</style>
